/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator;

import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.dtgenerator.api.BefApiGenerator;
import com.inspur.edp.bef.dtgenerator.config.BefConfigGenerator;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenerator;
import com.inspur.edp.bef.dtgenerator.core.rtgen.variable.BEVariableGenerator;
import com.inspur.edp.bef.dtgenerator.entity.BefEntityGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.module.ModuleGenerator;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import com.inspur.edp.cef.generator.apiGenerator.commonModel.CommonModelApiGenerator;
import com.inspur.edp.cef.generator.configGenerator.CefConfigGenerator;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGenerator;
import com.inspur.edp.cef.generator.entityGenerator.entity.CommonModelEntityGenerator;
import com.inspur.edp.cef.generator.overall.commonModel.CommonModelGenerator;
import com.inspur.edp.cef.generator.overall.commonModel.CommonModelGeneratorContext;
import com.inspur.edp.cef.generator.repository.RepositoryGenerator;
import com.inspur.edp.cef.variable.dtgenerator.VariableGenerator;
import com.inspur.edp.cef.variable.dtgenerator.VariableGeneratorContext;
import com.inspur.edp.jittojava.context.JitContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;

public class BEGenerator extends CommonModelGenerator {

  private boolean preGenerating;

  public BEGenerator(
      String path,
      GspMetadata metadata,
      boolean preGenerating,
      ModuleGenerator refGenerator,
      JitContext metadataJITContext) {
    super(path, metadata, preGenerating, refGenerator, metadataJITContext);
    this.preGenerating = preGenerating;

    test();
  }

  private void test() {

  }

  public BEGenerator(
      String path,
      GspMetadata metadata,
      com.inspur.edp.bef.dtgenerator.BEGeneratorContext genCtx,
      ModuleGenerator refGenerator,
      JitContext metadataJITContext) {
    super(path, metadata, genCtx, refGenerator, metadataJITContext);
  }

// keyword:
  // ORIGINAL LINE: internal new BEGeneratorContextgetContext() { return
  // (BEGeneratorContext)super.getContext(); }
  @Override
  public BEGeneratorContext getContext() {
    return (BEGeneratorContext) super.getContext();
  }

  @Override
  protected boolean getHasRepository() {
    return false;// getContext().getBE().getCategory() != BECategory.DependentBusinessEntity;
  }

  // region ovverided methods
  @Override
  protected void beforeInitialize() {
    super.beforeInitialize();
    getContext().setBE((GspBusinessEntity) metadata.getContent());
    DataValidator.checkForEmptyString(
        getContext().getBE().getGeneratingAssembly(), "GspBusinessEntity.GeneratingAssembly");
    // getContext().RelativeGeneratePath = ModelUtil.PrepareGenPath(getContext().MetadataPath);
    // getContext().SetGenPath(Path.Combine(PathTransform.GetUserProjectsRoot(),
    // getContext().RelativeGeneratePath));
    // TODO: java版临时注释 deb优先级低
    //		if (!DotNetToJavaStringHelper.isNullOrEmpty(getContext().getBE().getDependentEntityId()))
    //		{
    //
    //	getContext().setParentBE((GspBusinessEntity)getContext().getMetadataContent(getContext().getBE().getDependentEntityPackageName(), getContext().getBE().getDependentEntityId()));
    //			DataValidator.checkForNullReference(getContext().getParentBE(), "parentBeMetadata");
    //			DataValidator.checkForEmptyString(getContext().getParentBE().getGeneratingAssembly(),
    // "parentGSPBusinessEntity.GeneratingAssembly");
    //			if (getContext().isMetadataExistInCurrentProject(getContext().getBE().DependentEntityId))
    //			{
    //				// ① 同工程，不需要生成dbe
    //				getContext().setIsParentBEInSameProj(true);
    //			}
    //			else
    //			{
    //				// ② 不同工程，不需要生成dbe，需要添加引用
    //				getContext().setIsParentBEInSameProj(false);
    //
    //	getContext().setParentBEAssembly(getContext().GetAssemblyByMetadataId(getContext().getBE().DependentEntityId));
    //			}
    // var parentMetadata =
    // ServiceManager.GetService<IRefCommonService>().GetRefMetadata(getContext().BE.DependentEntityId);
    // DataValidator.CheckForNullReference(parentMetadata, "parentMetadata");
    // getContext().ParentBE = (GspBusinessEntity)parentMetadata.Content;
    // DataValidator.CheckForNullReference(getContext().ParentBE, "parentBeMetadata");
    // DataValidator.CheckForEmptyString(getContext().ParentBE.GeneratingAssembly,
    // "parentGSPBusinessEntity.GeneratingAssembly");
    // var parentBEGenCtx = new BEGeneratorService().InnerGenerate(getContext().MetadataPath,
    // parentMetadata, true, serverPath, refGenerator);
    // getContext().ParentBEGenContext = parentBEGenCtx;
    //		}
  }

  @Override
  protected CommonModelApiGenerator createApiGenerator() {
    return new BefApiGenerator(getContext());
  }

  @Override
  protected CommonModelCoreGenerator createCoreGenerator() {
    return new BefCoreGenerator(getContext());
  }

  @Override
  protected CommonModelEntityGenerator createEntityGenerator() {
    return new BefEntityGenerator(getContext());
  }

  @Override
  protected CommonModelGeneratorContext createCMGenContext() {
    BEGeneratorContext tempVar = new BEGeneratorContext();
//    tempVar.setServerPath(serverPath);
    //		tempVar.setCoreAssemblyName(super.metadataJITContext.ProjectInfoList[1].AssemblyName);
    return tempVar;
  }

  @Override
  protected RepositoryGenerator getCMRepositoryGenerator() {
    return null;
  }

  @Override
  protected CefConfigGenerator createConfigGenerator() {
    return new BefConfigGenerator(getContext());
  }

  @Override
  protected java.util.ArrayList<String> getExtendRefPaths() {
    java.util.ArrayList<String> list = new java.util.ArrayList<String>();
    // todo:wj-generator-ExtendRefPaths临时屏蔽
    // if (string.IsNullOrEmpty(getContext().ParentGeneratePath) == false)
    //             list.Add(getContext().ParentGeneratePath);
    if (DotNetToJavaStringHelper.isNullOrEmpty(getContext().getGeneratePath()))
    // throw new Exception();
    {
    }
    list.add(getContext().getGeneratePath());
    return list;
  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    //		new ProjectRefGenerator(getContext()).Generate();

    // if (!preGenerating)
    //    new DeployConfigGenerator(getContext()).Generate();
    // todo:wj-api生成完毕后，赋值ResultContext, 加工程引用
    // ResultContext = new GeneratorCommonContext
    //         {
    //             GeneratedPath = getContext().GeneratePath,
    //             RelativeGeneratedPath = getContext().RelativeGeneratePath,
    //	//todo-wj-EntityTypes临时注释
    //	//EntityTypes = getContext().DataInterfaceTypes
    // };

    // Copy2Bin();
    // if (getContext().PreGenerating)
    //    new ProjectRefGenerator(getContext(), metadata).Generate();
  }

  // endregion


  @Override
  protected java.util.ArrayList<BaseGenerator> createCefExtendGenerators() {
    if (!getContext().hasVariables()) {
      return null;
    }

    java.util.ArrayList<BaseGenerator> rez = new java.util.ArrayList<BaseGenerator>();
    BEVariableGenerator varGenerator;
    if (preGenerating) {
      varGenerator = new BEVariableGenerator(path,
          getContext().getBE().getVariables(),
          getContext(), preGenerating, getMetadataJITContext());
      getMetadataJITContext().getExtendProperty()
          .put(getContext().getBE().getID() + "VariableGen", varGenerator);
    } else {
      VariableGeneratorContext varGeneratorContext = ((VariableGenerator) ((getMetadataJITContext()
          .getExtendProperty().get(getContext().getBE().getID() + "VariableGen"))))
          .getVariableGeneratorContext();
      varGenerator = new BEVariableGenerator(
          path,
          getContext().getBE().getVariables(),
          getContext(),
          getMetadataJITContext().getMetadata(),
          varGeneratorContext,
          getMetadataJITContext().getCoreModuleGenerator(),
          getMetadataJITContext());
      getContext().setVarGenCtx(varGeneratorContext);
    }
    varGenerator.setParentContext(getContext());
    rez.add(varGenerator);
    return rez;
  }
}
